Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Quest NPC / TalkQuest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TalkQuest : MonoBehaviour
{
    public GameObject chatPrefab;
    public GameObject chatBoxPrefab;
    public GameObject buttonPrefab;

    private Quest quest;
    private RangedArea rangedArea;

    private RectTransform WorldcanvasTransform;
    private RectTransform UIcanvasTransform;

    private GameObject chat;
    private GameObject chatBox;
    private GameObject button;
    private GameObject button2;

    private bool declined;
    [HideInInspector] public bool accepted;

    [HideInInspector] public bool NPCchatEnabled;

    private string[] npcPhrases;
    private int npcPhrasesIndex;

    private Vector3 pos;

    // Start is called before the first frame update
    void Start()
    {
        rangedArea = FindObjectOfType<RangedArea>();
        WorldcanvasTransform = GameObject.FindGameObjectWithTag("WorldCanvas").GetComponent<RectTransform>();
        UIcanvasTransform = GameObject.FindGameObjectWithTag("UICanvas").GetComponent<RectTransform>();

        npcPhrasesIndex = 0;

        NPCchatEnabled = false;
        accepted = false;
        declined = false;
        //Cursor.visible = false;

    }

    private void chatTalk()
    {
        //Debug.Log(quest.questActivated);
        pos = new Vector3(transform.position.x, transform.position.y, transform.position.z);

        if ((Input.GetKeyDown(KeyCode.E)) && (rangedArea.playerInRange == true) && (NPCchatEnabled == false)  /*(quest.questActivated == false)*/)
        {

            chatBox = Instantiate(chatBoxPrefab, new Vector3(pos.x, pos.y + 2, pos.z), Quaternion.identity, WorldcanvasTransform);
            chat = Instantiate(chatPrefab, new Vector3(pos.x, pos.y + 2.3f, pos.z), Quaternion.identity, WorldcanvasTransform);
            button = Instantiate(buttonPrefab, new Vector3(pos.x + 2, pos.y + 1.5f, pos.z), Quaternion.identity, WorldcanvasTransform);

            npcPhrases = new string[] { "Hello, i need your help!",
                "There is a huge monster in the forest.",
                "can you kill him for me?"};

            chat.GetComponent<TypeWriting>().textToWrite = npcPhrases[npcPhrasesIndex];

            button.GetComponentInChildren<Text>().text = "-->";

            NPCchatEnabled = true;
            //Cursor.visible = true;

            button.GetComponent<Button>().onClick.AddListener(TestArray);

        }
        
        else if ((rangedArea.playerInRange == false) || (Input.GetKeyDown(KeyCode.E) && NPCchatEnabled == true) || declined == true || accepted == true)
        {
            Object.Destroy(chat);
            Object.Destroy(chatBox);
            Object.Destroy(button);
            Object.Destroy(button2);

            NPCchatEnabled = false;
            declined = false;
            //Cursor.visible = false;
        }

    }

    private void TestArray()
    {
        if (npcPhrasesIndex < npcPhrases.Length - 1)
        {

            Destroy(chat);
            chat = Instantiate(chatPrefab, new Vector3(pos.x, pos.y + 2.3f, pos.z), Quaternion.identity, WorldcanvasTransform);
      
            npcPhrasesIndex++;
            chat.GetComponent<TypeWriting>().textToWrite = npcPhrases[npcPhrasesIndex];
            chat.GetComponent<Text>().text = npcPhrases[npcPhrasesIndex];
            Debug.Log(npcPhrasesIndex);
        }

        if (npcPhrasesIndex == 2)
        {
            Destroy(button);
            button = Instantiate(buttonPrefab, new Vector3(pos.x + 2, pos.y + 1.5f, pos.z), Quaternion.identity, WorldcanvasTransform);
            button2 = Instantiate(buttonPrefab, new Vector3(pos.x - 2, pos.y + 1.5f, pos.z), Quaternion.identity, WorldcanvasTransform);

            button.GetComponent<Button>().onClick.AddListener(AcceptQuest);
            button2.GetComponent<Button>().onClick.AddListener(DeclineQuest);

            button.GetComponentInChildren<Text>().text = "Yes";
            button2.GetComponentInChildren<Text>().text = "No";
        }
    }
    private void AcceptQuest()
    {
        accepted = true;
    }

    private void DeclineQuest()
    {
        declined = true;
        npcPhrasesIndex = 0;
    }

    void Update()
    {
        chatTalk();
    }
}